package com.mysiteforme.admin.controller.email;

import com.xiaoleilu.hutool.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mysiteforme.admin.entity.EmailList;
import com.mysiteforme.admin.service.email.EmailService;
import com.mysiteforme.admin.util.other.LayerData;
import com.mysiteforme.admin.util.other.RestResponse;
import com.baomidou.mybatisplus.plugins.Page;
import com.mysiteforme.admin.annotation.SysLog;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 设置邮件  前端控制器
 * </p>
 */
@Controller
@RequestMapping("/admin/email")
public class EmailController {
    private static final Logger LOGGER = LoggerFactory.getLogger(EmailController.class);

    @Autowired
    private EmailService emailService;

    @GetMapping("list")
    @SysLog("跳转设置邮件列表")
    public String list(){
        return "/admin/email/list";
    }
    @RequiresPermissions("email:list")
    @PostMapping("list")
    @ResponseBody
    @SysLog("请求设置邮件列表数据")
    public LayerData<EmailList> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                      @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                      ServletRequest request){
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        LayerData<EmailList> layerData = new LayerData<>();
        EntityWrapper<EmailList> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag",false);
        if(!map.isEmpty()){
            String name = (String) map.get("name");
            if(StringUtils.isNotBlank(name)) {
                wrapper.like("name",name);
            }else{
                map.remove("name");
            }

            String emailAddress = (String) map.get("emailAddress");
            if(StringUtils.isNotBlank(emailAddress)) {
                wrapper.like("email_address",emailAddress);
            }else{
                map.remove("emailAddress");
            }

            String department = (String) map.get("department");
            if(StringUtils.isNotBlank(department)) {
                wrapper.like("department",department);
            }else{
                map.remove("department");
            }

        }
        Page<EmailList> pageData = emailService.selectPage(new Page<>(page,limit),wrapper);
        layerData.setData(pageData.getRecords());
        layerData.setCount(pageData.getTotal());
        return layerData;
    }

    @GetMapping("add")
    @SysLog("跳转新增设置邮件页面")
    public String add(){
        return "/admin/email/add";
    }
    @RequiresPermissions("email:add")
    @PostMapping("add")
    @SysLog("保存新增设置邮件数据")
    @ResponseBody
    public RestResponse add(EmailList email){
        if(StringUtils.isBlank(email.getName())){
            return RestResponse.failure("姓名不能为空");
        }
        if(StringUtils.isBlank(email.getEmailAddress())){
            return RestResponse.failure("邮件地址不能为空");
        }
        emailService.insert(email);
        return RestResponse.success();
    }

    @GetMapping("edit")
    @SysLog("跳转编辑设置邮件页面")
    public String edit(Long id,Model model){
        EmailList email = emailService.selectById(id);
        model.addAttribute("email",email);
        return "/admin/email/edit";
    }
    @RequiresPermissions("email:edit")
    @PostMapping("edit")
    @ResponseBody
    @SysLog("保存编辑设置邮件数据")
    public RestResponse edit(EmailList email){
        if(null == email.getId() || 0 == email.getId()){
            return RestResponse.failure("ID不能为空");
        }
        if(StringUtils.isBlank(email.getName())){
            return RestResponse.failure("姓名不能为空");
        }
        if(StringUtils.isBlank(email.getEmailAddress())){
            return RestResponse.failure("邮件地址不能为空");
        }
        emailService.updateById(email);
        return RestResponse.success();
    }
    @RequiresPermissions("email:delete")
    @PostMapping("delete")
    @ResponseBody
    @SysLog("删除设置邮件数据")
    public RestResponse delete(@RequestParam(value = "id",required = false)Long id){
        if(null == id || 0 == id){
            return RestResponse.failure("ID不能为空");
        }
        EmailList email = emailService.selectById(id);
        email.setDelFlag(true);
        emailService.updateById(email);
        return RestResponse.success();
    }

}